package net.borlis.configuration.context.apache;

import net.borlis.configuration.common.ApplicationConfiguration;
import net.borlis.configuration.common.ConfigurationInitError;
import net.borlis.configuration.common.DatabaseDriver;
import org.apache.commons.lang.builder.ToStringBuilder;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author alex@borlis.net
 */
@ApplicationScoped  @ContextData @Deprecated
public class ContextConfiguration implements ApplicationConfiguration {

    //todo:: tempo solution

    @Inject
    private Logger logger;

    //-=-=-=-
    private String jdbcUrl;

    /**
     * Init the configuration with the provided configuration file.
     */
    @PostConstruct
    public void init() {
        try {
            logger.info("configuration loading started");
            initJdbc();
            logger.info("configuration loading finished");
        } catch (Exception cex) {
            logger.log(Level.SEVERE, cex.getMessage(), cex);
            throw new ConfigurationInitError(cex);
        }
    }

    private void initJdbc() {
        final String urlPrefix;
        if(ConfigurationStorage.getData() == null){
            throw new IllegalStateException("configuration not found");
        }
        if (DatabaseDriver.MYSQL_DRIVER.getDriverName().equals(ConfigurationStorage.getData().getDatabaseDriver())) {
            urlPrefix = "jdbc:mysql://";
        } else {
            throw new IllegalStateException("unknown db driver");
        }
        jdbcUrl = urlPrefix + ConfigurationStorage.getData().getDatabaseServer() + ":"
                + ConfigurationStorage.getData().getDatabasePort() + "/"
                + ConfigurationStorage.getData().getSchemaVersion() + "_";
    }


    /**
     * {@inheritDoc}
     */
    @Override
    public String getJdbcUrlDraft() {
        init();
        return jdbcUrl;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getJdbcUser() {
        init();
        return ConfigurationStorage.getData().getDatabaseUser();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getJdbcPassword() {
        init();
        return ConfigurationStorage.getData().getDatabasePassword();
    }

    @Override
    public String getDriverName() {
        init();
        return ConfigurationStorage.getData().getDatabaseDriver();
    }

    @Override
    public String getSchemaVersion() {
        init();
        return ConfigurationStorage.getData().getSchemaVersion();
    }

    @Override
    public String getServer() {
        init();
        return ConfigurationStorage.getData().getDatabaseServer();
    }

    @Override
    public String getPort() {
        init();
        return ConfigurationStorage.getData().getDatabasePort();
    }

    @Override
    public Boolean getAutoUpdate() {
        init();
        return ConfigurationStorage.getData().getDatabaseAutoUpdate();
    }

    @Override
    public String getEncryptionServiceURL() {
        init();
        return ConfigurationStorage.getData().getEncryptionServiceURL();
    }

    @Override
    public String getEncryptionServiceLogin() {
        init();
        return ConfigurationStorage.getData().getEncryptionServiceLogin();
    }

    @Override
    public String getEncryptionServicePassword() {
        init();
        return ConfigurationStorage.getData().getEncryptionServicePassword();
    }

    @Override
    public String getClientKey() {
        init();
        return ConfigurationStorage.getData().getClientKey();
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this).
                append("jdbcUrl", getJdbcUrlDraft()).
                append("jdbcUser", getJdbcUser()).
                append("schemaVersion", getSchemaVersion()).
                append("driverName", getDriverName()).
                append("autoUpdate", getAutoUpdate()).
                append("encServiceLogin", getEncryptionServiceLogin()).
                append("encServiceURL", getEncryptionServiceURL()).
                append("clientKey", getClientKey()).
                toString();
    }

}
